package com.lhy.admin.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lhy.admin.common.mvcplus.java8time.Java8TimeObjectMapperFactory;
import com.lhy.admin.common.mvcplus.java8time.StringToLocalDateConverter;
import com.lhy.admin.common.mvcplus.java8time.StringToLocalDateTimeConverter;
import com.lhy.admin.common.secure.filter.AuthenticationInterceptor;
import com.lhy.admin.common.secure.filter.AuthorizationInterceptor;
import com.lhy.admin.common.secure.filter.CrosFilter;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * {@inheritDoc}
     * </br>注册 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthenticationInterceptor());
        registry.addInterceptor(new AuthorizationInterceptor());
    }

    @Bean
    public FilterRegistrationBean<CrosFilter> crosFilter() {
        FilterRegistrationBean<CrosFilter> registration = new FilterRegistrationBean<>(new CrosFilter());
        Map<String, String> pa = new HashMap<>();
        pa.put("allowOrigin", "http://localhost:3000");
        registration.setInitParameters(pa);
        registration.addUrlPatterns("/*");
        return registration;
    }

    @Bean("stringToLocalDateConverter")
    public Converter<String, LocalDate> stringToLocalDateConverter() {
        return new StringToLocalDateConverter();
    }

    @Bean("stringToLocalDateTimeConverter")
    public Converter<String, LocalDateTime> stringToLocalDateTimeConverter() {
        return new StringToLocalDateTimeConverter();
    }

    /**
     * 不能用内置的objectMapper 没法修改Serializer序列化规则，因为内置的objectMapper 的javaTimeModule已经注册过一次默认"yyyy-MM-ddTHH:mm:ss格式的
     * 再次注册无用
     *
     * @return
     */
    @Bean
    public ObjectMapper objectMapper() {
        return Java8TimeObjectMapperFactory.newObjectMapper();
    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public WxMpMessageRouter wxMpMessageRouter(WxMpService wxMpService) {
        return  new WxMpMessageRouter(wxMpService);
    }
}


